<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .wrap {
            width: 1226px;
            height: 460px;
            margin: 0 auto;
            margin-top: 50px;
            position: relative;
        }

        nav {
            display: flex;
            position: absolute;
            right: 40px;
            bottom: 20px;
            z-index: 9999;
        }

        nav li {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin: 0 6px;
            flex: 1;
            border: 1px solid #333;
            background-color: rgba(0, 0, 0, .4);
        }

        nav .active {
            background-color: rgba(255, 255, 255, 0.4);
        }

        section {
            /* height: 400px; */
            line-height: 400px;
            text-align: center;
            position: relative;
        }

        section>div {
            transition: all 0.25s;
            position: absolute;
            top: 0;
            opacity: 0;
        }

        section>div img {
            width: 100%;
        }

        section .show {
            opacity: 1;
            z-index: 1;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <nav>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </nav>
        <section>
            <div class="show"><a href="https://www.baidu.com"><img src="../images/1.jpg" alt=""></a></div>
            <div><a href="https://www.jd.com"><img src="../images/2.webp" alt=""></a></div>
            <div><a href="https://www.taobao.com"><img src="../images/3.webp" alt=""></a></div>
            <div><a href="https://www.bilibili.com"><img src="../images/4.webp" alt=""></a></div>
        </section>
    </div>
</body>
<script>
    // 简易轮播 -> 淡入淡出切换改写
    // 1. 包裹图片的容器 定位到一起 
    // 2. nav(绝对定位) section中的div(绝对定位)  => 无法撑开父元素 => 给父元素定高度
    // 3. section > div 默认最后一个层级最高  => 哪一个显示,将显示的盒子层级条高

    var wrap = document.querySelector(".wrap");

    var liList = document.querySelectorAll("nav li");
    console.log(liList);

    var partList = document.querySelectorAll("section>div");
    console.log(partList);


    // 自动切换  => 下标有规律的切换 (每隔3s - 5s自动切换)
    var index = 0;  //全局变量 (记录轮播的下标 默认显示第一张)
    var timer = null; // 全局变量 记录计时器的编号



    autoPlay(); //页面加载时 启动轮播


    // 点击切换  
    for (var i = 0; i < liList.length; i++) { // 循环绑定事件 
        var li = liList[i]; // 遍历每个li
        li.setAttribute("data-index", i);

        li.onclick = function () { //给遍历的li绑定事件 

            index = this.dataset.index; // 点击时导航对应的下标 => 赋值全局 => 也会影响轮播对应的下标

            for (var j = 0; j < liList.length; j++) { //  先清除所有导航的活跃状态,让所有part盒子隐藏
                liList[j].className = "";
                partList[j].className = "";
            }

            liList[index].className = "active";  //当前被点击的元素 变为活跃状态
            partList[index].className = "show";
        }
    }



    wrap.onmouseover = function () {
        console.log("移入...wrap");
        clearInterval(timer); // 移入暂停 
    }

    wrap.onmouseout = function () {
        console.log("移除...wrap");  //已经清除的计时器无法重启 只能启动一个新的,记录新的编号
        autoPlay(); //移出启动
    }



    function autoPlay() {
        clearInterval(timer);
        timer = setInterval(function () {
            index++; // 每隔3s 下标切换下一张 0 1 2 3
            if (index >= partList.length) {
                index = 0;
            }
            console.log(index);

            for (var j = 0; j < liList.length; j++) { //  先清除所有导航的活跃状态,让所有part盒子隐藏
                liList[j].className = "";
                partList[j].className = "";
            }

            liList[index].className = "active";  //当前被点击的元素 变为活跃状态
            partList[index].className = "show";

        }, 3000)
    }





</script>

</html>